package com.imy.common.view.recyclerview.adpater;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.imy.common.R;
import com.imy.common.view.recyclerview.DisplayableItem;
import com.imy.common.view.recyclerview.FooterViewHolder;
import com.imy.common.view.recyclerview.adapterdelegates.AdapterDelegate;
import com.imy.common.view.recyclerview.adapterdelegates.AdapterDelegatesManager;

import java.util.List;

/**
 * 混合适配器（有上拉更多）
 *
 * @author: 黄一凡
 * @date: 2017-03-21
 */
public class BaseMixMoreAdapter extends RecyclerView.Adapter {
  private AdapterDelegatesManager<List<DisplayableItem>> delegatesManager;
  protected List<DisplayableItem> items;
  public static final int ITEM_TYPE_FOOTER = 4371;
  private LayoutInflater mInflater;
  private boolean hasMore = true;
  private boolean hideMore = false;
  private String loadingTitle;

  private boolean isCategory;
  private boolean isWhite;

  public BaseMixMoreAdapter(Activity activity, List<DisplayableItem> items) {
    this.items = items;
    mInflater = LayoutInflater.from(activity);
    // Delegates
    delegatesManager = new AdapterDelegatesManager<>();
  }

  protected void addDelegate(AdapterDelegate<List<DisplayableItem>> delegate) {
    delegatesManager.addDelegate(delegate);
  }

  @Override
  public int getItemViewType(int position) {
    if (position == items.size()) {
      return ITEM_TYPE_FOOTER;
    }
    return delegatesManager.getItemViewType(items, position);
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == ITEM_TYPE_FOOTER) {
      FooterViewHolder footerViewHolder = new FooterViewHolder(
          mInflater.inflate(R.layout.view_more_loading, parent, false));
      return footerViewHolder;
    }
    return delegatesManager.onCreateViewHolder(parent, viewType);
  }

  @Override
  public void onBindViewHolder( RecyclerView.ViewHolder holder,  int position) {
    if (!(holder instanceof FooterViewHolder)) {
      delegatesManager.onBindViewHolder(items, position, holder);
    } else {
      if (hideMore) {
        hideMore((FooterViewHolder) holder);
      } else {
        if (hasMore) {
          setHasMore((FooterViewHolder) holder);
        } else {
          setNoMore((FooterViewHolder) holder);
        }
      }
    }
  }

  @Override
  public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position, List payloads) {
    if (!(holder instanceof FooterViewHolder)) {
      delegatesManager.onBindViewHolder(items, position, holder, payloads);
    } else {
      if (hideMore) {
        hideMore((FooterViewHolder) holder);
      } else {
        if (hasMore) {
          setHasMore((FooterViewHolder) holder);
        } else {
          setNoMore((FooterViewHolder) holder);
          ((FooterViewHolder) holder).rootViewOther.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              if (null == onFootViewClickListener) return;
              onFootViewClickListener.onFootViewClick(position,((FooterViewHolder) holder).rootViewOther);
            }
          });
        }
      }
    }
  }

  @Override
  public int getItemCount() {
    return items.size() + 1;
  }

  private void setNoMore(FooterViewHolder holder) {
    holder.rootView.setVisibility(View.VISIBLE);
    holder.pb.setVisibility(View.GONE);
    holder.promptText.setVisibility(View.VISIBLE);
    if(isWhite){
      holder.rootView.setBackgroundResource(R.color.smokewhite);
    }
    if(loadingTitle !=null){
      holder.promptText.setText(loadingTitle);
      if(isCategory){
        holder.rootViewOther.setVisibility(View.VISIBLE);
        holder.rootView.setVisibility(View.GONE);
      }else {
        holder.rootViewOther.setVisibility(View.GONE);
      }
    }else {
      holder.promptText.setText("别撩啦，到底啦~⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄");
    }
  }

  private void setHasMore(FooterViewHolder holder) {
    holder.rootView.setVisibility(View.VISIBLE);
    holder.pb.setVisibility(View.VISIBLE);
    holder.promptText.setVisibility(View.VISIBLE);
    holder.promptText.setText("正在加载中...");
  }

  private void hideMore(FooterViewHolder holder) {
    holder.rootView.setVisibility(View.GONE);
  }

  @Override
  public void onAttachedToRecyclerView(RecyclerView recyclerView) {

    final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
      final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
      final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

      gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
          if (position == items.size()) {
            return ((GridLayoutManager) layoutManager).getSpanCount();
          }
          if (spanSizeLookup != null) {
            return spanSizeLookup.getSpanSize(position);
          }
          return 1;
        }
      });
      gridLayoutManager.setSpanCount(gridLayoutManager.getSpanCount());
    }
  }

  @Override
  public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
    int position = holder.getLayoutPosition();
    if (position == items.size()) {
      ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();

      if (lp != null
          && lp instanceof StaggeredGridLayoutManager.LayoutParams) {

        StaggeredGridLayoutManager.LayoutParams p =
            (StaggeredGridLayoutManager.LayoutParams) lp;

        p.setFullSpan(true);
      }
    }
  }


  private OnFootViewClickListener onFootViewClickListener;

  public void setOnFootViewClickListener(OnFootViewClickListener onFootViewClickListener){
    this.onFootViewClickListener = onFootViewClickListener;
  }

  public interface OnFootViewClickListener{
    public void onFootViewClick(int position, View itemView);
  }

  public void setHasMore(boolean hasmore) {
    this.hasMore = hasmore;
  }

  public boolean isHideMore() {
    return hideMore;
  }

  public void setHideMore(boolean hideMore) {
    this.hideMore = hideMore;
  }

  public void setLoadingTitle(String title) {
    loadingTitle = title;
  }

  public void setCategory(boolean isCategory){
    this.isCategory=isCategory;
  }
  public void setBackColor(boolean isWhite){
    this.isWhite=isWhite;
  }
}
